function [varargout] = showvtcsize(varargin)
% showvtcsize  - displays the sizes of VTCs found on the given path
%
% FORMAT:       [s] = showvtcsize(folder, [pattern, ...])
%
% Input fields:
%
%       folder      folder where to look for VTCs
%       pattern     pattern
%       ...         options passed to findfiles
%
% Output fields:
%
%       s           1xN struct with fields
%        .filename  VTC filename
%        .protocol  linked protocol
%        .res       VTC resolution
%        .size      VTCData size
%        .xyzstart  start coordinate

% Version:  v0.7c
% Build:    7120607
% Date:     Dec-06 2007, 7:55 AM CET
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% initialize output first
s = cell2struct(cell(1, 0, 5), ...
    {'filename', 'protocol', 'res', 'size', 'xyzstart'}, 3);

% check arguments
if nargin < 1 || ...
   ~ischar(varargin{1}) || ...
    isempty(varargin{1})
    fld = pwd;
    v = findfiles(fld, '*.vtc', 'relative=');
elseif exist(varargin{1}(:)', 'dir') == 7
    fld = varargin{1}(:)';
    if nargin == 1 || ...
       ~ischar(varargin{2}) || ...
        isempty(varargin{2})
        try
            v = findfiles(fld, '*.vtc', 'relative=', varargin{2:end});
        catch
            rethrow(lasterror);
        end
    else
        if any(varargin{2}(:) == '*')
            try
                v = findfiles(fld, varargin{2}, 'relative=', varargin{3:nargin});
            catch
                rethrow(lasterror);
            end
        else
            try
                v = findfiles(fld, 'relative=', varargin{2:nargin});
            catch
                rethrow(lasterror);
            end
        end
    end
elseif any(varargin{1}(:) == '*')
    fld = pwd;
    try
        v = findfiles(fld, 'relative=', varargin{:});
    catch
        rethrow(lasterror);
    end
else
    error( ...
        'BVQXtools:BadArgument', ...
        'Invalid pattern or directory name given.' ...
    );
end

% check output
for vc = numel(v):-1:1
    if isempty(regexpi(v{vc}, '\.vtc$'))
        v(vc) = [];
    end
end
if isempty(v)
    return;
end

% enable BVQXfile transio for VTCs
vtctio = BVQXfile(0, 'transiosize', 'vtc');
BVQXfile(0, 'transiosize', 'vtc', 65536);

% iterate over files
s(numel(v)).filename = v{end};
for vc = 1:numel(s)
    
    % set filename
    s(vc).filename = v{vc};
    
    % load VTC
    try
        vtc = [];
        vtc = BVQXfile([fld '/' v{vc}]);
        if ~isBVQXfile(vtc, 'vtc')
            error('NO_VTC');
        end
    catch
        s(vc).filename = '';
        if isBVQXfile(vtc)
            vtc.ClearObject;
            continue;
        end
    end
    
    % get settings
    s(vc).protocol = vtc.NameOfLinkedPRT;
    s(vc).res = vtc.Resolution;
    s(vc).size = size(vtc.VTCData);
    s(vc).xyzstart = [vtc.XStart, vtc.YStart, vtc.ZStart];
    
    % clear object
    vtc.ClearObject;
end

% set transio size back
BVQXfile(0, 'transiosize', 'vtc', vtctio);

% clean output list and get max name sizes
mxf = 8;
mxp = 8;
for vc = numel(s):-1:1
    if isempty(s(vc).filename)
        s(vc) = [];
        continue;
    end
    mxf = max(mxf, numel(s(vc).filename));
    mxp = max(mxp, numel(s(vc).protocol));
end

% show sizes or return with struct
if nargout < 1
    mxf = min(mxf, 56);
    mxp = min(mxp, 56);
    mxf = sprintf(' %%-%ds | %%-%ds | ', mxf, mxp);
    sout = sprintf( ...
        [mxf 'Volumes | Dimensions  | Res | Start'], 'Filename', 'Protocol');
    disp(sout);
    sout(:) = '-';
    disp(sout);
    mxf = [mxf '%4d    | %3d,%3d,%3d | %2d  | %3d,%3d,%3d'];
    for vc = 1:numel(s)
        fn = s(vc).filename;
        if numel(fn) > 56
            fn = [fn(1:24) '...' fn(end-24:end)];
        end
        pt = s(vc).protocol;
        if numel(pt) > 56
            pt = [pt(1:24) '...' pt(end-24:end)];
        end
        disp(sprintf(mxf, fn, pt, s(vc).size(1), ...
            s(vc).size(2), s(vc).size(3), s(vc).size(4), s(vc).res, ...
            s(vc).xyzstart(1), s(vc).xyzstart(2), s(vc).xyzstart(3)));
    end
else
    varargout = cell(1, nargout);
    varargout{1} = s;
end
